{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import os\n",
    "from typing import List"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "我们经常需要读取一些类似Text的数据文件。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def load_gps_data(directory: str) -> pd.DataFrame:\n",
    "    \"\"\"\n",
    "    加载目录中所有 .plt 文件的 GPS 数据并合并到一个 DataFrame\n",
    "    \n",
    "    Args:\n",
    "        directory (str): 包含 .plt 文件的目录路径\n",
    "    \n",
    "    Returns:\n",
    "        pd.DataFrame: 合并后的 GPS 数据\n",
    "    \"\"\"\n",
    "    all_data = []\n",
    "    \n",
    "    # 遍历目录中的所有文件\n",
    "    for filename in os.listdir(directory):\n",
    "        if filename.endswith('.plt'):\n",
    "            file_path = os.path.join(directory, filename)\n",
    "            try:\n",
    "                # 调用 read_plt 函数读取单个文件\n",
    "                df = read_plt(file_path)\n",
    "                # 添加文件名作为用户标识（假设每个文件属于不同用户）\n",
    "                df['user'] = os.path.splitext(filename)[0]\n",
    "                all_data.append(df)\n",
    "            except Exception as e:\n",
    "                print(f\"Error loading {filename}: {e}\")\n",
    "    \n",
    "    # 合并所有 DataFrame\n",
    "    if all_data:\n",
    "        combined_data = pd.concat(all_data, ignore_index=True)\n",
    "        return combined_data\n",
    "    else:\n",
    "        print(\"No valid .plt files found in the directory.\")\n",
    "        return pd.DataFrame()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 使用示例\n",
    "if __name__ == \"__main__\":\n",
    "    # 设置数据目录路径\n",
    "    data_directory = \"path/to/your/gps_data\"  # 请替换为实际目录路径\n",
    "    \n",
    "    # 加载数据\n",
    "    gps_data = load_gps_data(data_directory)\n",
    "    \n",
    "    # 打印数据基本信息\n",
    "    print(f\"共加载 {len(gps_data)} 条 GPS 数据点\")\n",
    "    print(gps_data.head())\n",
    "    \n",
    "    # 可选：保存为 CSV 文件以便后续分析\n",
    "    gps_data.to_csv(\"combined_gps_data.csv\", index=False)"
   ]
  }
 ],
 "metadata": {
  "language_info": {
   "name": "python"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
